package com.winkyle.woodenfish.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.winkyle.woodenfish.BR;
import com.winkyle.woodenfish.R;
import com.winkyle.woodenfish.adapter.holder.Holder;
import com.winkyle.woodenfish.entity.MessageInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * @author winkyle
 * @date 2018/06/21
 * @description  Message消息列表的RecyclerView适配器类
 */

public class MessageAdapter extends RecyclerView.Adapter<Holder>{
    private Context context;
    private List<MessageInfo> messageInfos;
    private LayoutInflater layoutInflater;

    /**
     * @description
     * @param context 上下文
     * @return
     */
    public MessageAdapter(Context context) {
        this.context = context;
        this.messageInfos = new ArrayList<>();
        layoutInflater=LayoutInflater.from(context);
    }

    /**
     * @description 加入一条消息信息
     * @param messageInfo 消息信息
     * @return
     */
    public void addMessageInfo(MessageInfo messageInfo){
        messageInfos.add(messageInfos.size(),messageInfo);
        notifyItemInserted(messageInfos.size());
    }

    /**
     * @description 加载Item界面，绑定ViewHolder以便操作
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding;
        binding = DataBindingUtil.inflate(layoutInflater, R.layout.item_message,parent,false);
        return new Holder(binding);
    }

    /**
     * @description 进行数据绑定及数据显示操作，监听加此
     * @param holder
     * @param position
     * @return
     */
    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final MessageInfo messageInfo = messageInfos.get(position);
        if(messageInfo!=null) {
            holder.getBinding().setVariable(BR.messageInfo, messageInfo);
            holder.getBinding().setVariable(BR.onClickLinkListener, onClickLinkListener);
            holder.getBinding().executePendingBindings();
        }
    }

    /**
     * @description RecyclerView显示的条数
     * @return
     */
    @Override
    public int getItemCount() {
        return messageInfos==null?0:messageInfos.size();
    }

    /**
     * @description 获取当前消息列表
     * @return
     */
    public List<MessageInfo> getMessageInfos() {
        return messageInfos;
    }

    /**
     * @description 清除消息列表
     * @return
     */
    public void clearMessageInfos(){
        messageInfos.clear();
        notifyDataSetChanged();
    }

    private OnClickLinkListener onClickLinkListener;

    /**
     * @description 设置链接点击监听
     * @param onClickLinkListener
     * @return
     */
    public void setOnClickLinkListener(OnClickLinkListener onClickLinkListener) {
        this.onClickLinkListener = onClickLinkListener;
    }

    /**
     * @description  MessageAdapter适配器的点击监听
     */
    public interface OnClickLinkListener{
        /**
         * @description 链接被点击调用
         * @return
         */
        void onClick(MessageInfo messageInfo);
    }
}
